<template>
  <div>
    <!-- 面包屑导航区域 -->
    <breadcrumb :breadcrumbs="breadcrumbs" />

    <!-- 卡片区域 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <!-- 搜索框 -->
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getAllOrders"
          >
            <template #append>
              <el-button icon="el-icon-search"></el-button>
            </template>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表 -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否付款">
          <template v-slot="scope">
            <el-tag type="danger" v-if="scope.row.pay_status === '0'">
              未付款
            </el-tag>
            <el-tag type="success" v-else> 已付款 </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="订单发货" prop="is_send"></el-table-column>
        <el-table-column label="下单时间">
          <template v-slot="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <!-- 修改 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showAddresDialog"
            ></el-button>
            <!-- 定位 -->
            <el-button
              type="success"
              icon="el-icon-location"
              size="mini"
              @click="showProgressBox"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页  -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
    <!-- 修改地址弹出框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="addressVisible"
      width="50%"
      @close="addressDialogClosed"
    >
      <el-form
        :model="addresForm"
        :rules="addresFormRules"
        ref="addresFormRef"
        label-width="100px"
      >
        <el-form-item label="省市县/区" prop="address1">
          <el-cascader
            :options="cityData"
            v-model="addresForm.address1"
          ></el-cascader>
        </el-form-item>

        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addresForm.address2"></el-input>
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addressVisible = false">取 消</el-button>
          <el-button type="primary" @click="addressVisible = false">
            确 定
          </el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 物流信息对话框 -->
    <el-dialog title="物流进度" :visible.sync="progressVisible" width="50%">
      <el-timeline>
        <el-timeline-item
          v-for="(item, index) in ProgressList"
          :key="index"
          :timestamp="item.ftime"
        >
          {{ item.context }}
        </el-timeline-item>
      </el-timeline>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addressVisible = false">取 消</el-button>
          <el-button type="primary" @click="addressVisible = false">
            确 定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import Breadcrumb from "@/components/Breadcrumb";
import { getOrders } from "@/network/order/order";
import cityData from "./citydata";
import ProgressData from "./progressdata";

export default {
  name: "Order",
  data() {
    return {
      breadcrumbs: ["订单管理", "订单列表"],
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      orderList: [],
      addressVisible: false, //控制修改地址对话框
      addresForm: {
        address1: [],
        address2: "",
      },
      addresFormRules: {
        address1: [
          { required: true, message: "请选择省市县/区", trigger: "blur" },
        ],
        address2: [
          { required: true, message: "请选择详细地址", trigger: "blur" },
        ],
      },
      cityData, //省市县数据
      progressVisible: false, //物流信息对话框
      ProgressData,
      ProgressList: ProgressData.data,
    };
  },
  components: {
    Breadcrumb,
  },
  methods: {
    getAllOrders() {
      getOrders(this.queryInfo)
        .then((res) => {
          if (res.meta.status != 200)
            return this.$message.error("获取订单列表失败！" + res.meta.msg);
          this.$message.success("获取订单列表成功！");
          this.orderList = res.data.goods;
          this.total = res.data.total;
        })
        .catch((error) => {
          this.$message.error("获取订单列表失败！" + error);
        });
    },

    //监听页数大小变化
    handleSizeChange(newPageSize) {
      this.queryInfo.pagesize = newPageSize;
      this.getAllOrders();
    },
    //监听页码变更
    handleCurrentChange(newPageNum) {
      this.queryInfo.pagenum = newPageNum;
      this.getAllOrders();
    },
    //修改地址对话框弹出
    showAddresDialog() {
      this.addressVisible = true;
    },
    //监听对话框关闭
    addressDialogClosed() {
      this.$refs.addresFormRef.resetFields();
      this.addressVisible = false;
    },
    showProgressBox() {
      // getOrderProgress(1106975712662).then((res) => {
      //   if (res.meta.status != 200)
      //     return this.$message.error("获取物流进度失败！" + res.meta.msg);
      //   this.ProgressInfo = res.data;
      //   this.$message.success("获取物流进度成功！");
      // });
      console.log(this.ProgressList);
      this.progressVisible = true;
    },
  },
  created() {
    this.getAllOrders();
  },
};
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>